﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace qdata
{
    public class HtmlHandler
    {
        public  string GetHtmlCode(string url)
        {
            try
            {
                HttpWebRequest httpReq;
                HttpWebResponse httpResp;
                Uri httpurl = new System.Uri(url);

                httpReq = (HttpWebRequest)WebRequest.Create(httpurl);
                httpReq.Method = "GET";
                httpReq.KeepAlive = false; // 获取或设置一个值，该值指示是否与Internet资源建立持久连接。
                httpResp = (HttpWebResponse)httpReq.GetResponse();
                StreamReader reader = new StreamReader(httpResp.GetResponseStream(), GetLgFormat() );//System.Text.Encoding.GetEncoding("GB2312"));   //用GB2312字符
                string respHTML = reader.ReadToEnd();
                return respHTML;
            }
            catch 
            {
                return string.Empty;
            }                
        }
        public  Encoding GetLgFormat()
        {
            string LGFormat = "";
            if (LGFormat == "UTF-8")
            {
                return Encoding.UTF8;
            }
            return Encoding.GetEncoding("GB2312");

        }
    }
}
